import oglFixme
from OpenGL.arrays import vbo
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from point import Point
from numpy import array
import sys, math, os


EXIT = -1
FIRST = 0
points = []
vb_obj = None

def init(width, height):
   """ Initialize an OpenGL window """
   glClearColor(0.0, 0.0, 0.0, 0.0)         #background color
   glMatrixMode(GL_PROJECTION)              #switch to projection matrix
   glLoadIdentity()                         #set to 1
   glOrtho(-1.5, 1.5, -1.5, 1.5, -1.0, 1.0) #multiply with new p-matrix
   glMatrixMode(GL_MODELVIEW)               #switch to modelview matrix


def display():
   """ Render all objects"""
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) #clear screen
   # Set color to light gray
   glColor3f(.75, .75, .75)
   # Set drawstyle
   glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
   # Set primitve type to polygon
   glBegin(GL_TRIANGLES)

   LENGTH = 6
   
   for i in range(LENGTH):
       glVertex3f(-6+i, 2*((i+1)%2 * -1), 0)
       glVertex3f(-5+i, 2*(i%2 * -1), 0)
       glVertex3f(-4+i, 2*((i+1)%2 * -1), 0)
       
   for i in range(LENGTH):
       glVertex3f(6-i, 2*((i+1)%2), 0)
       glVertex3f(5-i, 2*(i%2), 0)
       glVertex3f(4-i, 2*((i+1)%2), 0)
   
   glEnd()
   # Flush commands
   glFlush()
   glutSwapBuffers()



def reshape(width, height):
   """ adjust projection matrix to window size"""
   glViewport(0, 0, width, height)
   glMatrixMode(GL_PROJECTION)
   glLoadIdentity()
   if width <= height:
       glOrtho(-1.5, 1.5,
               -1.5*height/width, 1.5*height/width,
               -1.0, 1.0)
   else:
       glOrtho(-1.5*width/height, 1.5*width/height,
               -1.5, 1.5,
               -1.0, 1.0)
   glMatrixMode(GL_MODELVIEW)


def keyPressed(key, x, y):
   """ handle keypress events """
   if key == chr(27): # chr(27) = ESCAPE
       sys.exit()


def mouse(button, state, x, y):
   """ handle mouse events """
   if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN:
       print "left mouse button pressed at ", x, y


def mouseMotion(x,y):
   """ handle mouse motion """
   print "mouse motion at ", x, y


def menu_func(value):
   """ handle menue selection """
   print "menue entry ", value, "choosen..."
   if value == EXIT:
       sys.exit()
   glutPostRedisplay()


def readfile(filename):
   f = open(filename, 'r')
   point = []
   for line in f.readlines():
      point = line.split(' ')
      points.append(Point(point[0], point[1], point[2]))

   global points
   points = [[p.x, p.y, p.z] for p in points]
   # vertex buffer objekt (speicher auf graka) mit array von floats fuellen
   vb_obj = vbo.VBO(array(points, 'f'))
   


def main():
##   if len(sys.argv) != 2:
##      print "Falsche Parameterlaenge: " + len(sys.argv)
##      sys.exit(EXIT)
   
   # read file
   f_name = "bunny_points.raw"
   readfile(f_name)

   
   # Hack for Mac OS X
   cwd = os.getcwd()
   glutInit(sys.argv)
   os.chdir(cwd)


   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
   glutInitWindowSize(500, 500)
   glutCreateWindow("simple openGL/GLUT template")

   glutDisplayFunc(display)     #register display function
   glutReshapeFunc(reshape)     #register reshape function
   glutKeyboardFunc(keyPressed) #register keyboard function 
   glutMouseFunc(mouse)         #register mouse function
   glutMotionFunc(mouseMotion)  #register motion function
   #glutCreateMenu(menu_func)    #register menue function




   glutAddMenuEntry("First Entry",FIRST) #Add a menu entry
   glutAddMenuEntry("EXIT",EXIT)         #Add another menu entry
   glutAttachMenu(GLUT_RIGHT_BUTTON)     #Attach mouse button to menue

   init(500,500) #initialize OpenGL state

   glutMainLoop() #start even processing


if __name__ == "__main__":
   main()
